package middleware

import (
	"context"
	"fmt"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

type RpcAuthMiddleware struct {
}

func NewRpcAuthMiddleware() *RpcAuthMiddleware {
	return &RpcAuthMiddleware{}
}

func (m *RpcAuthMiddleware) Handle() grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
		// 计算消息处理时间
		start := time.Now()
		md, ok := metadata.FromIncomingContext(ctx)
		fmt.Println("给我 data: ", md, ok)

		resp, err = handler(ctx, req)
		logx.Infof("Request processed in %s", time.Since(start))
		return
	}
}
